package com.aiju.cash.ui.account.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.bean.Account;
import com.aiju.cash.R;
import com.aiju.widgit.dialog.AccountDialog;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-27 13:37
 * @version: V1.0
 * @Description: 描述
 */
public class AccountAdapter extends BaseAdapter {

    private boolean manager;
    private Context mContext;
    private List<Account> accounts;
    private LayoutInflater mLayoutInflater;

    public AccountAdapter(Context mContext, List<Account> accounts) {
        this.mContext = mContext;
        this.accounts = accounts;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setManager(boolean manager) {
        this.manager = manager;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return accounts != null ? accounts.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return accounts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Account account = accounts.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_account, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_account_pic);
            holder.nameTextView = (TextView) convertView.findViewById(R.id.tv_account_name);
            holder.defaultTextView = (TextView) convertView.findViewById(R.id.tv_account_default);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameTextView.setText(account.getName());
        String color = AccountDialog.colors.get(Integer.valueOf(account.getColor()));
        holder.imageView.setBackgroundColor(Color.parseColor(color));
        if (account.getIsDefault().equals("1")) {
            holder.defaultTextView.setTextColor(Color.parseColor("#999999"));
            holder.defaultTextView.setText("默认账本");
        } else {
            holder.defaultTextView.setTextColor(Color.parseColor("#00a0e8"));
            holder.defaultTextView.setText("设置为默认账本");
            holder.defaultTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onAccountClick!=null){
                        onAccountClick.onDefault(account);
                    }
                }
            });
        }
        if (manager) {
            convertView.findViewById(R.id.ly_account_menu).setVisibility(View.VISIBLE);
//            convertView.findViewById(R.id.iv_account_share).setVisibility(View.GONE);
            holder.defaultTextView.setVisibility(View.GONE);
        } else {
            convertView.findViewById(R.id.ly_account_menu).setVisibility(View.GONE);
//            convertView.findViewById(R.id.iv_account_share).setVisibility(View.VISIBLE);
            holder.defaultTextView.setVisibility(View.VISIBLE);
        }

        convertView.findViewById(R.id.iv_account_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAccountClick!=null){
                    onAccountClick.onDelete(account);
                }
            }
        });
        convertView.findViewById(R.id.iv_account_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAccountClick!=null){
                    onAccountClick.onShare(account);
                }
            }
        });
        convertView.findViewById(R.id.iv_account_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAccountClick!=null){
                    onAccountClick.onEdit(account);
                }
            }
        });
        return convertView;
    }

    private OnAccountClick onAccountClick;

    public void setOnAccountClick(OnAccountClick onAccountClick) {
        this.onAccountClick = onAccountClick;
    }

    public void setIsDefault(Account defaultAccount) {
        for (Account account:accounts) {
            if (account!=defaultAccount){
                account.setIsDefault("2");
            }
        }
        notifyDataSetChanged();
    }

    public interface OnAccountClick {

        void onDelete(Account account);

        void onEdit(Account account);

        void onShare(Account account);

        void onDefault(Account account);
    }

    static class ViewHolder {
        ImageView imageView;
        TextView nameTextView;
        TextView defaultTextView;
    }
}
